package com.yuyou.tas.report.controller;

import com.yuyou.tas.report.service.IExamModelReportService;
import com.yuyou.tas.report.service.IExamModelService;
import com.yuyou.tas.report.vo.req.ExamExcel;
import com.yuyou.tas.service.api.admin.entity.AuthUser;
import com.yuyou.tas.service.api.admin.service.AuthUserAPI;
import com.yuyou.tas.util.bean.LoginRedisObj;
import com.yuyou.tas.util.bean.ReturnResult;
import com.yuyou.tas.util.enums.admin.AuthUserEnums;
import com.yuyou.tas.util.tool.Webs;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.hibernate.validator.constraints.NotEmpty;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;

import java.io.InputStream;
import java.util.List;

@Api(value="DataHandleController",description="操作接口")
@Validated
@RestController
@RequestMapping(value="/api/dataHandle")
public class DataHandleController extends BaseController{

    @Autowired
    private IExamModelService examModelService;

    @Autowired
    private IExamModelReportService examModelReportService;

    @Autowired
    private AuthUserAPI authUserAPI;

    @ApiIgnore
    @ApiOperation("导入试卷数据")
    @ApiImplicitParam(name = "file", value = "文件", paramType="query",required = true)
    @RequestMapping(value = "/examImport", method = {RequestMethod.GET, RequestMethod.POST})
    public ReturnResult<String> examImport(@RequestParam(value="file") MultipartFile file,
                                           HttpServletRequest request) throws Exception {
        LoginRedisObj userObj = getUserObj(request);

        InputStream inputStream = file.getInputStream();
        ImportParams params = new ImportParams();

        List<ExamExcel> examExcelList = ExcelImportUtil.importExcel(inputStream, ExamExcel.class, params);
        examModelService.saveExamImport(userObj.getOrganizationId() ,examExcelList);

        return ReturnResult.success("OK");
    }

    @ApiIgnore
    @ApiOperation("导入成绩数据")
    @ApiImplicitParam(name = "file", value = "文件", paramType="query",required = true)
    @RequestMapping(value = "/examReportImport", method = {RequestMethod.GET, RequestMethod.POST})
    public ReturnResult<String> examReportImport(@RequestParam(value="file") MultipartFile file,
                                           HttpServletRequest request, HttpServletResponse response) throws Exception {
        LoginRedisObj userObj = getUserObj(request);

        InputStream inputStream = file.getInputStream();

        XSSFWorkbook xssfSheets = examModelReportService.saveExcel(inputStream , userObj.getOrganizationId());
//        XSSFWorkbook xssfSheets = examModelReportService.saveExcel(inputStream , 1015052939893653504l);
        Webs.writeExcel(request, response, xssfSheets , "导入失败信息表");

        return ReturnResult.success("OK");
    }

    @ApiOperation("导出成绩数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "examModelActivityIds", value = "活动Ids,逗号隔开", paramType = "query", required = true),
            @ApiImplicitParam(name = "baseSchoolIds", value = "校区Ids,逗号隔开", paramType = "query", required = false),
            @ApiImplicitParam(name = "baseTrimesterId", value = "学期Id", paramType = "query", required = false),
            @ApiImplicitParam(name = "baseSectionIds", value = "年级Ids,逗号隔开", paramType = "query", required = false),
            @ApiImplicitParam(name = "dataSubjectIds", value = "学科Ids,逗号隔开", paramType = "query", required = false)})
    @RequestMapping(value = "/examReportExport", method = {RequestMethod.GET, RequestMethod.POST})
    public ReturnResult<String> examReportExport(@RequestParam(value="examModelActivityIds",required = true)@NotEmpty(message = "活动ID不能为空")List<Long> examModelActivityIds,
                                                 @RequestParam(value="baseSchoolIds",required = false)List<Long> baseSchoolIds,
                                                 @RequestParam(value="baseTrimesterId",required = false)Long baseTrimesterId,
                                                 @RequestParam(value="baseSectionIds",required = false)List<Long> baseSectionIds,
                                                 @RequestParam(value="dataSubjectIds",required = false)List<Long> dataSubjectIds,
                                                 HttpServletRequest request, HttpServletResponse response) throws Exception {
        LoginRedisObj userObj = getUserObj(request);
        // 教务、班主任或管理员都能看到所有班级
        AuthUser authUser = authUserAPI.selectById(userObj.getUserId());
        Boolean isManage = (userObj.getIsManager() || AuthUserEnums.type.registrar.getValue() == authUser.getType()
                || AuthUserEnums.type.headmaster.getValue() == authUser.getType() || AuthUserEnums.type.other.getValue() == authUser.getType()) ? true : false;
//        HSSFWorkbook studentScoreExcel = examModelReportService.excelStudentScore(isManage, authUser.getId(), examModelActivityId, baseSchoolIds, baseTrimesterId, baseSectionIds, dataSubjectIds);
        HSSFWorkbook studentScoreExcel = examModelReportService.excelStudentScoreNew(isManage, authUser.getId(), examModelActivityIds, baseSchoolIds, baseTrimesterId, baseSectionIds, dataSubjectIds);
        Webs.writeHSSFExcel(request, response, studentScoreExcel , "学生成绩表");
        return ReturnResult.success("OK");
    }

    /*@ApiOperation("校验成绩数据")
    @ApiImplicitParam(name = "file", value = "文件", paramType="query",required = true)
    @RequestMapping(value = "/examReportCheck", method = {RequestMethod.GET, RequestMethod.POST})
    public ReturnResult<String> examReportCheck(@RequestParam(value="file") MultipartFile file,
                                                 HttpServletRequest request, HttpServletResponse response) throws Exception {
        LoginRedisObj userObj = getUserObj(request);

        InputStream inputStream = file.getInputStream();
        XSSFWorkbook xssfSheets = examModelReportService.check(inputStream , 1015052939893653504l);
//        examModelReportService.saveExcel(inputStream , userObj.getOrganizationId());
        Webs.writeExcel(request, response, xssfSheets , "导入失败信息表");
        return ReturnResult.success("ok");
//        return ReturnResult.success("OK");
    }*/
}
